package com.zhangxiaofanfan.arthas.controller;

import com.zhangxiaofanfan.arthas.service.ArthasService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * 学习 arthas 使用的控制器
 *
 * @author zhangxiaofanfan
 * @date 2025-03-11 10:00:13
 */
@Slf4j
@RestController
@RequestMapping("/arthas")
public class ArthasController {
    @Autowired
    private ArthasService arthasService;

    /**
     * 模拟异步异常请求
     *
     * @param taskId 任务id
     * @return 返回结果
     */
    @GetMapping("insert/void")
    public String asyncVoidException(@RequestParam("task_id") Integer taskId) {
        arthasService.insertTaskVoid(taskId);
        return "任务异步插入完成, 返回结果";
    }

    /**
     * 模拟异步异常请求
     *
     * @param taskId 任务id
     * @return 返回结果
     */
    @GetMapping("insert")
    public String asyncException(@RequestParam("task_id") Integer taskId) {
        CompletableFuture<Integer> future = arthasService.insertTask(taskId);
        return "任务异步插入完成, 返回结果";
    }
}
